package org.qs.webservice.controller;
 
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;  
import javax.ws.rs.Consumes;  
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;  
import javax.ws.rs.Path;  
import javax.ws.rs.core.Context;  
import javax.ws.rs.core.MediaType;    

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.IreadFileService;
import org.qs.webservice.util.IReadUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;




@Component
@Path("/v{version}/file")
public class FileRestController {
	@Resource
	IreadFileService ireadFileService;
	/**
	 * 上传头像
	 * @Title: uploadimage 
	 * @param request
	 * @param response
	 * @param token
	 * @return   
	 * @return String    
	 * @throws
	 */
	@POST  
	@Path("/portrait")  
	@Consumes(MediaType.MULTIPART_FORM_DATA)  
	public String uploadPortrait(@Context HttpServletRequest request, @Context HttpServletResponse response,@HeaderParam("token")String token) {  
		try{ 
			MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
			// 获得文件：   
			MultipartFile file1 = multipartRequest.getFile("file");   
			
			byte[] bytes = file1.getBytes();
		    String str=ireadFileService.saveFaceImage(bytes, token);
			return str;
  
		
		
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			QSReadException.printf(e, "上传头像异常！",errorJson);
		}
	
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","参数错误");	
		
	
		return IReadUtils.beanToJson(tokenMap);
		
		

	} 
	
	
	
	
}
